package com.yitao.cms.service.print;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.print.PrintRecodeDto;
import com.yitao.client.model.print.PrintRecodeModel;
import com.yitao.client.model.print.QPrintRecodeModel;
import com.yitao.cms.utils.DateUtils;
import com.yitao.cms.vo.order.PrintRecodeVo;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: TUO
 * @Date: 2019/7/3 11:12
 * @Description:
 */
@Service
public class PrintRecodeService extends GenericEntityService<PrintRecodeModel, PrintRecodeDto, QPrintRecodeModel> {
    public PrintRecodeService(EntityManager entityManager) {
        super(entityManager);
    }


    public List<PrintRecodeVo> getPrintRecode(String packageCode){
        List<PrintRecodeVo> itemList = new ArrayList<>();
        List<PrintRecodeModel> list = queryFactory.select(Q).from(Q).where(Q.packageCode.eq(packageCode)).orderBy(new OrderSpecifier<>(Order.DESC, Q.createTime)).fetch();
        itemList = list.stream().map(p -> {
            PrintRecodeVo printRecodeVo = new PrintRecodeVo();
            printRecodeVo.setMachineCode(p.getMachineCode());
            printRecodeVo.setPackageCode(p.getPackageCode());
            printRecodeVo.setToken(p.getToken());
            printRecodeVo.setCreateTime(DateUtils.getDateFormat(p.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
            return printRecodeVo;
        }).collect(Collectors.toList());
        return itemList;
    }

}
